package com.easyexcel231130.demos;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.enums.CellExtraTypeEnum;
import com.alibaba.fastjson.JSONObject;
import com.easyexcel231130.demos.dto.ExcelDemoDTO;
import com.easyexcel231130.demos.listener.DemoDataListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@Slf4j
public class Demo1Controller {
    /**
     * 读取excel信息1
     * @param base
     */
    @PostMapping("/demo1")
    public List<ExcelDemoDTO> demo1(@RequestParam("base") MultipartFile base) throws IOException {
        DemoDataListener<ExcelDemoDTO> listener = new DemoDataListener<>();
        EasyExcel.read(base.getInputStream(), ExcelDemoDTO.class,listener)
                // 需要读取批注 默认不读取
                .extraRead(CellExtraTypeEnum.COMMENT)
                // 需要读取超链接 默认不读取
                .extraRead(CellExtraTypeEnum.HYPERLINK)
                // 需要读取合并单元格信息 默认不读取
                .extraRead(CellExtraTypeEnum.MERGE).sheet(0).doRead();
        List<ExcelDemoDTO> list = listener.getList();
        log.info("解析到的数据：{}", JSONObject.toJSONString(list));
        return list;
    }
}
